
# load packages
library(tidyverse)
library(haven)

# load data
raw_df <- read_dta("data/cleaned GTE data.dta") %>%
  glimpse() 

# ----------
# clean primary data
# ----------

# create a gathered df for treatment
treat_df <- raw_df %>%
  select(pid, respid, starts_with("treat")) %>%
  gather(treat, treat_indicator, starts_with("treat")) %>%
  mutate(issue_id = str_extract(treat, "[0-9]+")) %>%
  glimpse()

# create a gathered df for agree
agree_df <- raw_df %>%
  select(pid, respid, starts_with("agree")) %>%
  gather(agree, agree_measure, starts_with("agree")) %>%
  mutate(issue_id = str_extract(agree, "[0-9]+")) %>%
  glimpse()

# comine dfs
df <- left_join(treat_df, agree_df) %>%
  filter(!is.na(agree_measure)) %>%
  select(respid, issue_id, agree_measure, treat_indicator) %>%
  glimpse() %>%
  write_rds("output/respondent-data.rds")

# sanity check
df %>%
  group_by(respid) %>%
  summarize(n = n()) %>%
  glimpse()
